role_message();
function role_message() {
    $.ajax({
        url: '/Role.do',
        method: 'get',
        success: function (result) {

            if (result && result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    $(`<option>${val}</option>`).appendTo('select');
                }
            }
        }
    });
}



$('#my_file').change(function () {
    view(this.files);
})
function view(file_list) {
    // console.log(file_list);
    //遍历图片列表
    for (let f of file_list) {
        //创建文件读取器
        let reader = new FileReader();
        //读取图片
        reader.readAsDataURL(f);
        //在onload事件(读取完成并成功)
        reader.onload = function (data) {
            // console.log(data.target.result + 'aaaa');
            //将图片数据给img标签
            $('#my_photo').attr('src', data.target.result);
            $('#my_box figcaption').hide();
            // console.log($('#my_photo'.src));
        }
    }
}

//通过拖拽添加图片
var box = $('#my_box')[0];
box.ondragover = function (e) {
    e.preventDefault();
}
box.ondrop = function (e) {
    e.preventDefault();
    view(e.dataTransfer.files);
    document.querySelector('#my_file').files = e.dataTransfer.files;
    // console.log(e.dataTransfer.files);
    console.log($('#my_file').attr('files'));
}

//添加员工
$('#alter_btn').click(function () {
    let name = $('#name').val().trim();
    let age = $('#age').val();
    let sex = $('input[name=sex]:checked').val();
    let phone = $('#phone').val();
    let idCard = $('#idCard').val();
    let role = $('#role_select').val();
    let photo = document.querySelector('#my_file').files[0];
    if (name != '' && age != '' && phone != '' && idCard != '') {
        try{
            photo.name != '';
        }catch(error){
            swal('预览图片失败', '添加失败', 'error');
            return;
        }
        
    }else{
        swal('个人信息不完整', '添加失败', 'error');
        return;
    }
    swal({
        title: '确定添加吗？',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定添加！',
    }).then(function (result) {
        if (!result.value) { return; }

        var formData = new FormData();
        formData.append('name', name);
        formData.append('age', age);
        formData.append('sex', sex);
        formData.append('phone', phone);
        formData.append('idCard', idCard);
        formData.append('role', role);
        formData.append('photo', photo);
        // console.log(formData)
        $.ajax({
            url: '/alterEmpls.do',
            method: 'post',
            data: formData,
            processData: false,
            contentType: false,
            success: function (result) {
                if (result == 'success') {
                    location.href = 'employee.html'
                }
            }
        });
    })




});

$('#cel_btn').click(function () {
    location.href = 'employee.html'
});

//修改员工信息

getParams();
function getParams() {

    let id = location.search.split('=')[1];
    if (!id) {
        $('#my_title').html('添加员工信息');

    } else {
        $('#my_title').html('修改员工信息');
        $('#alter_btn').html('修改');
        $.get('/queryEmplsById', { id }, function (empls) {
            $('#name').val(empls.name);
            $('#age').val(empls.age);
            $('#phone').val(empls.phone);
            $('#idCard').val(empls.idCard);
            $('#role_select').val(empls.role);
            $('input[value=' + empls.sex + ']').prop('checked', true);
            $('#my_photo').attr('src', '/assets/img/' + empls.photo);
            $('#my_photo').siblings('figcaption').hide();
        });
        // $('#alter_btn'), cklck(function () {
        //     let name = $('#name').val().trim();
        //     let age = $('#age').val();
        //     let sex = $('input[name=sex]:checked').val();
        //     let phone = $('#phone').val();
        //     let idCard = $('#idCard').val();
        //     let role = $('#role_select').val();
        //     let photo = document.querySelector('#my_file').files[0];
        //     var formData = new FormData();
        //     formData.append('name', name);
        //     formData.append('age', age);
        //     formData.append('sex', sex);
        //     formData.append('phone', phone);
        //     formData.append('idCard', idCard);
        //     formData.append('role', role);
        //     formData.append('photo', photo);
        // });
    }
}